#region Usings
using System;
using BK.Entities;
using Lib.ApplicationBlocks.DbBlocks.Membership;
using Lib.Data.CriteriaRoutine;
using Lib.Data.ORM;
using Lib.Data.ORM.EntityManagement;
#endregion

namespace BK.BL
{
	[DataManager]
	public class Users : BaseGeneralDataManager<UserEntity>, IMgrMemberships
	{
		#region C'tors
		public Users(BaseOrmDataBase ormDataBase) : base(ormDataBase)
		{
		}
		#endregion

		#region IMgrMemberships Members
		public IMembership Load(string login, string password)
		{
			return LoadSingle(new Criteria("Login", Clause.Eq, login).And("Password", Clause.Eq, password));
		}

		public IMembership Load(string login)
		{
			return LoadSingle(new Criteria("Login", Clause.Eq, login));
		}

		IMembership IMgrMemberships.Save(IMembership membership)
		{
			Update((UserEntity) membership);
			return membership;
		}

		public void SetCulture(string userName, CultureAndLocal cultureAndLocal)
		{
			UserEntity userEntity = (UserEntity)Load(userName);
			userEntity.CultureName = cultureAndLocal.CultureName;
			userEntity.LocaleName = cultureAndLocal.LocaleName;
			Save(userEntity);
		}

		public CultureAndLocal GetCulatuCultureAndLocal(string userName)
		{
			UserEntity userEntity = (UserEntity) Load(userName);

			if (!string.IsNullOrEmpty(userEntity.CultureName) && !string.IsNullOrEmpty(userEntity.LocaleName))
				return new CultureAndLocal {CultureName = userEntity.CultureName, LocaleName = userEntity.LocaleName};

			return null;
		}
		#endregion
	}
}